package com.xiumu.service.blog.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiumu.common.core.exception.blog.BlogException;
import com.xiumu.common.core.page.PageQuery;
import com.xiumu.common.core.utils.AssertUtil;
import com.xiumu.dao.blog.BlogPostDao;
import com.xiumu.pojo.blog.dto.BlogPostDTO;
import com.xiumu.pojo.blog.entity.BlogPost;
import com.xiumu.pojo.blog.entity.BlogPostTag;
import com.xiumu.pojo.blog.query.BlogPostQuery;
import com.xiumu.service.blog.BlogPostService;
import com.xiumu.service.blog.BlogPostTagService;
import com.xiumu.service.blog.BlogTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文章信息 Service 接口实现
 *
 * @author XiuMu
 * @Date 2023-01-29 09:28:02
 */
@Service
public class BlogPostServiceImpl extends ServiceImpl<BlogPostDao, BlogPost> implements BlogPostService {

    @Autowired
    private BlogTagService blogTagService;

    @Autowired
    private BlogPostTagService blogPostTagService;

    @Override
    public Page<BlogPost> listPage(PageQuery<BlogPostQuery, BlogPost> pageQuery) {
        return this.baseMapper.selectPage(pageQuery.toPage(), pageQuery.getCondition());
    }

    @Override
    public List<BlogPost> listByBlogPost(BlogPostQuery blogPost) {
        return this.baseMapper.selectByBlogPost(blogPost);
    }

    @Override
    @Transactional
    public boolean create(BlogPostDTO blogPostDTO) {
        BlogPost blogPost = BeanUtil.copyProperties(blogPostDTO, BlogPost.class);
        this.save(blogPost);
        // 绑定标签
        if (CollUtil.isNotEmpty(blogPostDTO.getTagIdList())){
            List<BlogPostTag> postTagList = blogPostDTO.getTagIdList().stream().map(tag -> new BlogPostTag(blogPost.getId(), tag)).toList();
            blogPostTagService.saveBatch(postTagList);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updateById(BlogPostDTO blogPostDTO, Long id) {
        BlogPost blogPost = getById(id);
        AssertUtil.isNotNull(blogPost, BlogException.NOT_EXIT);
        BeanUtil.copyProperties(blogPostDTO, blogPost);
        blogPost.setId(id);
        blogPostTagService.updatePostTag(id, blogPostDTO.getTagIdList());
        return updateById(blogPost);
    }

    @Override
    public boolean bindTag(Long postId, String tagName) {
        BlogPost blogPost = getById(postId);
        AssertUtil.isNotNull(blogPost, BlogException.NOT_EXIT);
        Long tagId = blogTagService.getIdOrSaveByTagName(tagName);
        return blogPostTagService.save(new BlogPostTag(postId, tagId));
    }

    @Override
    public boolean deleteTag(Long postId, Long tagId) {
        return blogPostTagService.remove(new LambdaQueryWrapper<BlogPostTag>().eq(BlogPostTag::getPostId, postId).eq(BlogPostTag::getTagId, tagId));
    }
}




